
var page = {
  isRenderBg: true,
  data: {},
  time: 5 * 60 * 1000,
  point: 10,
  scroll: false,
  backMessage: '是否确认退出？',
  first: false,
  init: function (options) {
    for (var attr in options) {
      this[attr] = options[attr]
    }
    this._getBgs()
    this._addDomain()
    
    if (config.debug) {
      $('body').append('<div id="test" style="position:absolute;left: 30px;right: 30px;top: 30px; background: rgba(0,0,0,.5);color: #fff;font-size: 16px;word-break: break-word;"></div>')
    }

    try {
      document.addEventListener('plusready', function () {
        plus.key.addEventListener('backbutton', function () {
          var wv = plus.webview.currentWebview()
          var url = wv.getURL()
          if (url.indexOf('index') > -1) {
            if(confirm('确认退出？')){
              plus.runtime.quit();
            }
          } else  {
            wv.close()
          }
        })
      })
    } catch (error) {
      
    }
    
    utils.auth()
    this._startPoint()

  },
  // 观看获取积分
  _startPoint: function () {
    var _this  = this
    setInterval(function () {
      taskPoints({
        points: _this.point,
        desc: '观看'
      })
    }, this.time)
  },
  // 获取配置信息【背景、设置】
  _getBgs: function (){
    var data = getTreeByTypeId(config.typeids[0]).TYPEs
    this.data.bgs =  data
    this._renderBg()
  },
  // 渲染背景
  _renderBg: function () {
    if (!this.isRenderBg) return  
    try {
      var picture = page.data.bgs[2].typePicture[0].picPath3
      $("#main").css("background-image", "url(" + picture + ")");
    } catch (error) {
      console.log(error)
    }
  },
  

  addUrl: function (to, form) {
    // 兼容H5
    // try {
    //   //plus.webview.open(to)
    // } catch (error) {
    //   $("#test").html("error");
    //   if (form) {
    //     var time = new Date().getTime();
    //     addReturnPage(encodeURIComponent(form), time);
        
    //   }
    //   window.location.href = to
    // }
    if (form) {
      var time = new Date().getTime();
      addReturnPage(encodeURIComponent(form), time);
      
    }
    window.location.href = to


  },

  goback: function () {
    var to = getReturnPage();
    
    var url = '';
    if (to.data != null && to.data.page) {
      url = decodeURIComponent(to.data.page)
      window.location.href = url
    } else {
      this.goHome()
    };
  },
  goHome: function () {
    location.href = page.urls.home
  },
  _addDomain: function () {
    var url = location.href
    var return_index = url.indexOf('ReturnURL=');
    if (return_index > 0) {
      clearReturnPage();
      returnUrl = url.substring(url.indexOf('?') + 1);
      addDomainPath(escape(escape(returnUrl)))
      this.first = true
    } else {
      this.first = false
    }
    this._getDomain()
  },
  _getDomain: function () {
    var data = getDomainPath();
    var urls = {};
    if (data.code == 200 && data.data) {
      var page = unescape(data.data.page);
      var arrs = page.split('&')
      for (var i = 0, l = arrs.length; i < l; i++) {
        var arr = arrs[i].split('=')
        urls[arr[0]] = unescape(arr[1])
      } 
      if (urls.trueReturnURL) {
        urls.home = urls.trueReturnURL
        urls._domain = urls.ReturnURL.substring(0,urls.ReturnURL.lastIndexOf('?'));
        urls.domain = urls.ReturnURL.substring(0, urls.ReturnURL.lastIndexOf('/') + 1)
      } else if (urls.source) {
        urls.home = urls.source
        urls.domain = urls.ReturnURL + '/'
        if(urls.ReturnURL.indexOf("iptv") > 0 ){
          urls.ReturnURL += "portal.jsp";
        }else{
          urls.ReturnURL += "Category.jsp"
        }
        urls._domain = urls.ReturnURL;
      } else {
        urls.home = urls.ReturnURL
        urls.domain = urls.ReturnURL.substring(0, urls.ReturnURL.lastIndexOf('/') + 1)
        urls._domain = urls.ReturnURL.substring(0,urls.ReturnURL.lastIndexOf('?'));
      }
    }
    this.urls = urls
  }
};
